<?php
/*
 *	Coded by the global config develpment team.
 *	Homepage: http://global-config.com/
*/


class whois
{
	// Thanks to : http://www.nirsoft.net/whois_servers_list.html
	public static $whois_servers = array(
		'ac' => 'whois.nic.ac',
		'ae' => 'whois.nic.ae',
		'af' => 'whois.nic.af',
		'ag' => 'whois.nic.ag',
		'al' => 'whois.ripe.net',
		'am' => 'whois.amnic.net',
		'as' => 'whois.nic.as',
		'at' => 'whois.nic.at',
		'au' => 'whois.aunic.net',
		'az' => 'whois.ripe.net',
		'ba' => 'whois.ripe.net',
		'be' => 'whois.dns.be',
		'bg' => 'whois.register.bg',
		'bi' => 'whois.nic.bi',
		'biz' => 'whois.neulevel.biz',
		'bj' => 'www.nic.bj',
		'br' => 'whois.nic.br',
		'bt' => 'whois.netnames.net',
		'by' => 'whois.ripe.net',
		'bz' => 'whois.belizenic.bz',
		'ca' => 'whois.cira.ca',
		'cc' => 'whois.nic.cc',
		'cd' => 'whois.nic.cd',
		'ch' => 'whois.nic.ch',
		'ck' => 'whois.nic.ck',
		'cl' => 'nic.cl',
		'cn' => 'whois.cnnic.net.cn',
		'co.nl' => 'whois.co.nl',
		'com' => 'whois.verisign-grs.com',
		'coop' => 'whois.nic.coop',
		'cx' => 'whois.nic.cx',
		'cy' => 'whois.ripe.net',
		'cz' => 'whois.nic.cz',
		'de' => 'whois.denic.de',
		'dk' => 'whois.dk-hostmaster.dk',
		'dm' => 'whois.nic.cx',
		'dz' => 'whois.ripe.net',
		'edu' => 'whois.educause.net',
		'ee' => 'whois.eenet.ee',
		'eg' => 'whois.ripe.net',
		'es' => 'whois.ripe.net',
		'eu' => 'whois.eu',
		'fi' => 'whois.ficora.fi',
		'fo' => 'whois.ripe.net',
		'fr' => 'whois.nic.fr',
		'gb' => 'whois.ripe.net',
		'ge' => 'whois.ripe.net',
		'gl' => 'whois.ripe.net',
		'gm' => 'whois.ripe.net',
		'gov' => 'whois.nic.gov',
		'gr' => 'whois.ripe.net',
		'gs' => 'whois.adamsnames.tc',
		'hk' => 'whois.hknic.net.hk',
		'hm' => 'whois.registry.hm',
		'hn' => 'whois2.afilias-grs.net',
		'hr' => 'whois.ripe.net',
		'hu' => 'whois.ripe.net',
		'ie' => 'whois.domainregistry.ie',
		'il' => 'whois.isoc.org.il',
		'in' => 'whois.inregistry.net',
		'info' => 'whois.afilias.info',
		'int' => 'whois.isi.edu',
		'iq' => 'vrx.net',
		'ir' => 'whois.nic.ir',
		'is' => 'whois.isnic.is',
		'it' => 'whois.nic.it',
		'je' => 'whois.je',
		'jp' => 'whois.jprs.jp',
		'kg' => 'whois.domain.kg',
		'kr' => 'whois.nic.or.kr',
		'la' => 'whois2.afilias-grs.net',
		'li' => 'whois.nic.li',
		'lt' => 'whois.domreg.lt',
		'lu' => 'whois.restena.lu',
		'lv' => 'whois.nic.lv',
		'ly' => 'whois.lydomains.com',
		'ma' => 'whois.iam.net.ma',
		'mc' => 'whois.ripe.net',
		'md' => 'whois.nic.md',
		'me' => 'whois.nic.me',
		'mil' => 'whois.nic.mil',
		'mk' => 'whois.ripe.net',
		'mobi' => 'whois.dotmobiregistry.net',
		'ms' => 'whois.nic.ms',
		'mt' => 'whois.ripe.net',
		'mu' => 'whois.nic.mu',
		'mx' => 'whois.nic.mx',
		'my' => 'whois.mynic.net.my',
		'name' => 'whois.nic.name',
		'net' => 'whois.verisign-grs.com',
		'nf' => 'whois.nic.cx',
		'nl' => 'whois.domain-registry.nl',
		'no' => 'whois.norid.no',
		'nu' => 'whois.nic.nu',
		'nz' => 'whois.srs.net.nz',
		'org' => 'whois.pir.org',
		'pl' => 'whois.dns.pl',
		'pr' => 'whois.nic.pr',
		'pro' => 'whois.registrypro.pro',
		'pt' => 'whois.dns.pt',
		'ro' => 'whois.rotld.ro',
		'ru' => 'whois.ripn.ru',
		'sa' => 'saudinic.net.sa',
		'sb' => 'whois.nic.net.sb',
		'sc' => 'whois2.afilias-grs.net',
		'se' => 'whois.nic-se.se',
		'sg' => 'whois.nic.net.sg',
		'sh' => 'whois.nic.sh',
		'si' => 'whois.arnes.si',
		'sk' => 'whois.sk-nic.sk',
		'sm' => 'whois.ripe.net',
		'st' => 'whois.nic.st',
		'su' => 'whois.ripn.net',
		'tc' => 'whois.adamsnames.tc',
		'tel' => 'whois.nic.tel',
		'tf' => 'whois.nic.tf',
		'th' => 'whois.thnic.net',
		'tj' => 'whois.nic.tj',
		'tk' => 'whois.nic.tk',
		'tl' => 'whois.domains.tl',
		'tm' => 'whois.nic.tm',
		'tn' => 'whois.ripe.net',
		'to' => 'whois.tonic.to',
		'tp' => 'whois.domains.tl',
		'tr' => 'whois.nic.tr',
		'travel' => 'whois.nic.travel',
		'tw' => 'whois.apnic.net',
		'tv' => 'whois.nic.tv',
		'ua' => 'whois.ripe.net',
		'uk' => 'whois.nic.uk',
		'gov.uk' => 'whois.ja.net',
		'us' => 'whois.nic.us',
		'uy' => 'nic.uy',
		'uz' => 'whois.cctld.uz',
		'va' => 'whois.ripe.net',
		'vc' => 'whois2.afilias-grs.net',
		've' => 'whois.nic.ve',
		'vg' => 'whois.adamsnames.tc',
		'ws' => 'www.nic.ws',
		'yu' => 'whois.ripe.net'
	);
	
	private static function get_info($domain)
	{
		// Create an array of returns
		$return = array(
			'protocol' => null,
			'domain' => null,
			'tld' => null,
			'path' => null,
			'whois' => null
		);
		// Try to get the protocol
		$domain = explode('://', $domain);
		// Make sure that there will be two items
		// in the array
		if (isset($domain[1]))
		{
			$return['protocol'] = $domain[0];
			$domain = $domain[1];
		}
		else
		{
			$domain = $domain[0];
		}
		
		// Get the path
		$domain = explode('/', $domain);
		$return['domain'] = $domain[0];
		if (isset($domain[1]))
		{
			$domain = join('/', $domain);
			// Remove the domain from the path
			$domain = str_replace($return['domain'], '', $domain);
			$return['path'] = $domain;
			// Reset the domain
			$domain = $return['domain'];
		}
		else
		{
			$domain = $domain[0];
		}
		// Get the subdomains, domain and TLD.
		$domain = explode('.', $domain);
		$return['tld'] = $domain[count($domain)-1];
		
		// We don't need this anymore
		$domain = null;
		unset($domain);
		
		if (isset(self::$whois_servers[$return['tld']]))
		{
			$return['whois'] = self::$whois_servers[$return['tld']];
		}
		
		// Now, check if everything we NEED is set.
		// If the whois is empty, that means that one of the things
		// we need are empty, so no need to check the rest.
		if (empty($return['whois']))
		{
			return(false);
		}
		
		return($return);
		exit;
		
	}
	
	// Modified version of : http://www.anyexample.com/programming/php/php_whois_client_function.xml
	public static function execute($domain)
	{
		$server = self::get_info($domain);
		// Something's wrong with the domain they entered.
		if ($server == false)
		{
			return(false);
		}
		
		// Connect to the WHOIS server.
    	$f = fsockopen($server['whois'], 43, $whois_err[0], $whois_err[1], 15);
	    
		if (!$f) 
		{
        	return false;
		}
	    
		// Send the domain
    	fwrite($f, $server['domain']."\r\n");
	    
		// Get the response
    	$response = '';
	    while (!feof($f))
		{
			$response .= fgets($f, 1024);
		}
		
		// Close connection
	    fclose($f);
		
		// Check for errors
		if($whois_err[0] > 0)
		{
			return(false);
		}
		
		// Everything went okay, now return this to the user.
		return($response);
	}
	
}